package com.ch.android.baselib

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

/**
 * Author: conghao
 * Date: 2023/12/26 15:00
 * Description: BaseBindingFragment
 */
abstract class BaseBindingFragment<VDB : ViewDataBinding> : Fragment() {
    private var mBinding: VDB? = null
    protected val binding: VDB
        get() {
            return mBinding ?: throw IllegalStateException("错误：未初始化 mBinding")
        }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (mBinding == null) {
            Log.d("BaseBindingFragment", "onCreateView: mBinding == null")
            if (getLayout() == 0) {
                throw NotImplementedError("错误：未实现 getLayout() 方法！")
            }
            mBinding = DataBindingUtil.inflate<VDB>(inflater, getLayout(), container, false)

            mBinding?.lifecycleOwner = this
        }
        return mBinding?.root
    }

    abstract fun getLayout(): Int

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding?.unbind()
    }

}